package cc.co.zysoft;

import android.app.TabActivity;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.Spannable;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.webkit.WebView;
import android.widget.EditText;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;

public class MainActivity extends TabActivity implements OnTabChangeListener {

	// constants
	public final String EDITOR = "editor";
	public final String PREVIEW = "preview";
	public static final String PREFS_NAME = "DroidWebDevPrefs";

	// attributes
	private EditText editText;
	private WebView webView;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tab_host);

		Resources res = getResources(); // Resource object to get Drawables
		TabHost tabHost = getTabHost(); // The activity TabHost
		TabHost.TabSpec spec; // Re-usable TabSpec for each tab

		// Initialize a TabSpec for each tab and add it to the TabHost
		spec = tabHost.newTabSpec(EDITOR); // Editor tab
		spec.setIndicator("Editor", res.getDrawable(R.drawable.ic_tab_editor)); // tab
																				// styling
		spec.setContent(R.id.editorTab); // tab content
		tabHost.addTab(spec); // add tab to the TabHost

		// Same for Browser tab
		spec = tabHost.newTabSpec(PREVIEW); // Preview tab
		spec.setIndicator("Preview", res.getDrawable(R.drawable.ic_tab_preview)); // tab
																					// styling
		spec.setContent(R.id.wvPreview); // tab content
		tabHost.addTab(spec); // add tab to the TabHost

		// set the open tab to the Editor tab
		tabHost.setCurrentTabByTag(EDITOR);
		// add the OnTabChangeListener to the TabHost
		tabHost.setOnTabChangedListener(this);

		// turn on JavaScript in the preview browser
		webView = (WebView) findViewById(R.id.wvPreview);
		webView.getSettings().setJavaScriptEnabled(true);

		// add text changer to EditText for syntax highlighting
		editText = (EditText) findViewById(R.id.editText);
		// Restore preferences
		Log.i("onCreate", "Restoring data");
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		String data = settings.getString("data", "");
		editText.setText(data);
		editText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before,
					int count) {
				// TODO Auto-generated method stub

			}

			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub

			}

			@Override
			public void afterTextChanged(Editable s) {
				Log.i("afterTextChanged", "afterTextChanged has been called");
				// Fetch the code and add Spannables to the keywords
				String data = s.toString();
				Log.i("afterTextChanged", data);
				if (data.contains("<") && data.contains(">")) {
					Log.i("afterTextChanged", "contains < and >");
					int i = 0;
					while (i < data.length()) {
						int start = data.indexOf("<", i);
						int end = data.indexOf(">", start);
						if (start >= 0 && end >= 0) {
							Log.i("afterTextChanged", "adding spannable from "
									+ start + " to " + end);
							s.setSpan(new ForegroundColorSpan(Color.RED),
									start, end + 1,
									Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
							String tag = data.substring(start, end + 1);
//							if (tag.contains(" ")) {
//								Log.i("afterTextChanged", "tag contains space");
//								int j = 0;
//								while (j < tag.length()) {
//									int spaceIndex = tag.indexOf(" ", j);
//									int equalsIndex = tag.indexOf("=",
//											spaceIndex);
//									if (spaceIndex >= 0 && equalsIndex >= 0) {
//										s.setSpan(
//												new ForegroundColorSpan(
//														Color.GREEN),
//												spaceIndex,
//												equalsIndex,
//												Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//										int firstQuote = tag.indexOf("\"",
//												equalsIndex);
//										int secondQuote = tag.indexOf("\"",
//												firstQuote);
//										s.setSpan(
//												new ForegroundColorSpan(
//														Color.BLUE),
//												firstQuote,
//												secondQuote,
//												Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
//
//									}
//
//								}
//							}

							i = end;
						} else {
							i = data.length();
						}
					}
				}
			}
		});

	}

	// == save state info
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		String data = editText.getText().toString();
		outState.putString("data", data);
		Log.i("onSaveInstanceState data", data);

	}

	// == restore state info
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		String data = savedInstanceState.getString("data");
		editText.setText(data);
		Log.i("onRestoreInstanceState data", data);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.ActivityGroup#onStop()
	 */
	@Override
	protected void onStop() {
		super.onStop();
		// We need an Editor object to make preference changes.
		// All objects are from android.context.Context
		SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
		SharedPreferences.Editor editor = settings.edit();
		String data = editText.getText().toString();
		Log.i("onStop data", data);
		editor.putString("data", data);

		// Commit the edits!
		editor.commit();

		Log.i("onStop", "onStop has been called");
	}

	/*
	 * Code goes here for when a new tab is clicked
	 */
	@Override
	public void onTabChanged(String tabId) {
		if (tabId.equals(PREVIEW)) {
			String data = editText.getText().toString();
			webView.loadData(data, "text/html", null);
		}
	}

}
